
package edu.uts.reekie.designer;
import org.eclipse.jface.action.*;
import org.eclipse.ui.actions.*;
import org.eclipse.gef.*;
import org.eclipse.gef.ui.actions.*;
import edu.uts.reekie.designer.actions.*;


public abstract class AbstractContextMenuProvider
	extends org.eclipse.gef.ContextMenuProvider
{

private ActionRegistry actionRegistry;

public AbstractContextMenuProvider(EditPartViewer viewer, ActionRegistry registry) {
	super(viewer);
	setActionRegistry(registry);
}



/* (non-Javadoc)
 * @see org.eclipse.gef.ContextMenuProvider#menuAboutToShow(org.eclipse.jface.action.IMenuManager)
 */
public void buildContextMenu(IMenuManager manager) {
	GEFActionConstants.addStandardActionGroups(manager);
	

	IAction action;

	action = getActionRegistry().getAction(ActionFactory.UNDO.getId());
	manager.appendToGroup(GEFActionConstants.GROUP_UNDO, action);

	action = getActionRegistry().getAction(ActionFactory.REDO.getId());
	manager.appendToGroup(GEFActionConstants.GROUP_UNDO, action);

	action = getActionRegistry().getAction(ActionFactory.PASTE.getId());
	if (action.isEnabled())
		manager.appendToGroup(GEFActionConstants.GROUP_EDIT, action);

	action = getActionRegistry().getAction(ActionFactory.DELETE.getId());
	if (action.isEnabled())
		manager.appendToGroup(GEFActionConstants.GROUP_EDIT, action);

	action = getActionRegistry().getAction(GEFActionConstants.DIRECT_EDIT);
	if (action.isEnabled())
		manager.appendToGroup(GEFActionConstants.GROUP_EDIT, action);

	
	//
	// Component Actions
	//
	action = getActionRegistry().getAction(ToggleMultiplicityAction.MULTIPLICITY);
	if (action.isEnabled())
		manager.appendToGroup(GEFActionConstants.GROUP_REST, action);
	
	
	//
	// other context menu actions
	//
	addToContextMenu(manager);
	
	
	//
	// Connector Direction Actions
	//
	action = getActionRegistry().getAction(SetConnectorDirectionAction.BIDIRECTIONAL);
	if (action.isEnabled())
		manager.appendToGroup(GEFActionConstants.GROUP_REST, action);
	
	action = getActionRegistry().getAction(SetConnectorDirectionAction.UNIDIRECTIONAL);
	if (action.isEnabled())
		manager.appendToGroup(GEFActionConstants.GROUP_REST, action);
	
	
	
	
	//
	// Connector Labels
	//

	
	
	action = getActionRegistry().getAction(SetConnectorLabelAction.NORMAL);
	if (action.isEnabled())
		manager.appendToGroup(GEFActionConstants.GROUP_REST, action);
	action = getActionRegistry().getAction(ClearConnectorLabelAction.NORMAL);
	if (action.isEnabled())
		manager.appendToGroup(GEFActionConstants.GROUP_REST, action);
	
	action = getActionRegistry().getAction(SetSourceToTargetConnectorLabelAction.SOURCE_TO_TARGET);
	if (action.isEnabled())
		manager.appendToGroup(GEFActionConstants.GROUP_REST, action);

	action = getActionRegistry().getAction(ClearSourceToTargetConnectorLabelAction.SOURCE_TO_TARGET);
	if (action.isEnabled())
		manager.appendToGroup(GEFActionConstants.GROUP_REST, action);
	
	action = getActionRegistry().getAction(SetTargetToSourceConnectorLabelAction.TARGET_TO_SOURCE);
	if (action.isEnabled())
		manager.appendToGroup(GEFActionConstants.GROUP_REST, action);
	action = getActionRegistry().getAction(ClearTargetToSourceConnectorLabelAction.TARGET_TO_SOURCE);
	if (action.isEnabled())
		manager.appendToGroup(GEFActionConstants.GROUP_REST, action);

	//
	// Connector Other Actions
	//
	
	
	//
	// Alignment Actions
	//
	MenuManager submenu = new MenuManager(Messages.AlignmentAction_AlignSubmenu_ActionLabelText);

	action = getActionRegistry().getAction(GEFActionConstants.ALIGN_LEFT);
	if (action.isEnabled())
		submenu.add(action);

	action = getActionRegistry().getAction(GEFActionConstants.ALIGN_CENTER);
	if (action.isEnabled())
		submenu.add(action);

	action = getActionRegistry().getAction(GEFActionConstants.ALIGN_RIGHT);
	if (action.isEnabled())
		submenu.add(action);
		
	submenu.add(new Separator());
	
	action = getActionRegistry().getAction(GEFActionConstants.ALIGN_TOP);
	if (action.isEnabled())
		submenu.add(action);

	action = getActionRegistry().getAction(GEFActionConstants.ALIGN_MIDDLE);
	if (action.isEnabled())
		submenu.add(action);

	action = getActionRegistry().getAction(GEFActionConstants.ALIGN_BOTTOM);
	if (action.isEnabled())
		submenu.add(action);

	if (!submenu.isEmpty())
		manager.appendToGroup(GEFActionConstants.GROUP_REST, submenu);

	action = getActionRegistry().getAction(ActionFactory.SAVE.getId());
	manager.appendToGroup(GEFActionConstants.GROUP_SAVE, action);

}

protected abstract void addToContextMenu(IMenuManager manager);


protected ActionRegistry getActionRegistry() {
	return actionRegistry;
}

protected void setActionRegistry(ActionRegistry registry) {
	actionRegistry = registry;
}

}
